package cc.chenwenxi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

import cc.chenwenxi.sdk.demo.SystemInfoExample;
import cc.chenwenxi.sdk.rainbow.FanKit;
import cc.chenwenxi.sdk.rainbow.LedKit;
import cc.chenwenxi.sdk.rainbow.PiKit;
import cc.chenwenxi.sdk.rainbow.RgbKit;
import cc.chenwenxi.sdk.rainbow.RgbKit.Rgb;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class SaksSdkApplication {

	public static void main(String[] args) throws Exception {
//		new SystemInfoExample().info();
//		buzzer();
		// aaa();
//		rgb();
//		led();
		// key1();
		// demo();
		SpringApplication.run(SaksSdkApplication.class, args);
	}

	public static void fun() {
		System.out.println("风扇转");
		FanKit fan = new FanKit();
		fan.speed(500);
		ThreadUtil.sleep(1000 * 5);
		System.out.println("关闭fan");
		fan.speed(0);
	}

	public static void rgb() throws Exception {
		RgbKit rgbKit = new RgbKit();
		System.out.println("兰花的紫色");
		rgbKit.setRgb(new Rgb(218, 112, 214));
		System.out.println("兰花的紫色:" + rgbKit.getRgb());
		ThreadUtil.sleep(2000);

		System.out.println("深紫罗兰的蓝色");
		rgbKit.setRgb(new Rgb(138, 43, 226));
		System.out.println("深紫罗兰的蓝色:" + rgbKit.getRgb());
		ThreadUtil.sleep(2000);

		System.out.println("海军蓝");
		rgbKit.setRgb(new Rgb(0, 0, 128));
		System.out.println("海军蓝:" + rgbKit.getRgb());
		ThreadUtil.sleep(2000);

		System.out.println("深石板灰");
		rgbKit.setRgb(new Rgb(47, 79, 79));
		System.out.println("深石板灰:" + rgbKit.getRgb());
		ThreadUtil.sleep(2000);

		System.out.println("恢复默认");
		rgbKit.shutdown();

	}

	public static void key1() {
		GpioPinDigitalInput pinInput = GpioFactory.getInstance().provisionDigitalInputPin(RaspiPin.GPIO_22);
		// set shutdown state for this input pin
//		pinInput.setShutdownOptions(true);

		// create and register gpio pin listener
		pinInput.addListener(new GpioPinListenerDigital() {
			@Override
			public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
				// display pin state on console
				System.out.println(" 点击了--> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());

			}
		});
	}

	public static void buzzer() {
		PiKit.buzzer.high();
		log.info("叫~~~~~");
		ThreadUtil.sleep(1000);
		log.info("闭嘴~~~~~");
		PiKit.buzzer.low();
	}

	public static void led() {
		LedKit led = new LedKit();
		System.out.println("led1亮");
		led.led1_high();// 为什么low反而是亮,high是灭??
		ThreadUtil.sleep(1000 * 3);
		System.out.println("led1灭");
		led.led1_low();

		System.out.println("led2亮");
		led.led2_high();
		ThreadUtil.sleep(1000 * 3);
		System.out.println("led2灭");
		led.led2_low();

		System.out.println("led3亮");
		led.led3_high();
		ThreadUtil.sleep(1000 * 3);
		System.out.println("led3灭");
		led.led3_low();

		System.out.println("led4亮");
		led.led4_high();
		ThreadUtil.sleep(1000 * 3);
		System.out.println("led4灭");
		led.led4_low();
	}

//	public static void demo() throws IOException {
//		Transport transport = new SPITransport(SpiChannel.CS0, RaspiPin.GPIO_06, RaspiPin.GPIO_05);
//		SSD1306 ssd1306 = new SSD1306(128, 64, transport);
//		ssd1306.display();
//
//		Graphics graphics = ssd1306.getGraphics();
//		ssd1306.display();
//		graphics.text(0, 0, new CodePage1252(), "BtcLtcEthEtchaha");
//		ssd1306.display();
//		ThreadUtil.sleep(1000 * 5);
//		System.out.println("画画");
////		BufferedImage read = ImageUtil.read(new URL("http://hbimg.b0.upaiyun.com/cbb2bd58bbcb0748fc2cfed5630842af2c8d3fab3c8d-J2gTQG_fw236"));
////		graphics.image(read, 0, 0, 64, 64);
////		ssd1306.display();
//		System.out.println("打印hello");
//	}

}
